import grails.converters.JSON
class ClassesAjaxController {

    def index = { }
    def getCourseTeacher = {
        log.info(params.courseName)
        def teacherList = Teacher.withCriteria {            
            eq('state', false)
            eq('enabled', true)
            eq('school', School.get(params.school))
        }

        def courseTeacherList;
        courseTeacherList = teacherList.grep{
            it.course.toList().any{ elem ->
                params.courseName == elem
            }
        }
        log.info(courseTeacherList)
        if(courseTeacherList.size() > 0){
            return render(['success':true,
            'message' : courseTeacherList] as JSON)             
        }else{
            return render(['success':false,
            'message' : "没有可以带此课程的老师！"] as JSON) 
        }
    }

   //移动学生到某个班级下
   def removeStudentsToClasses = {
        Classes newClasses = Classes.get(params.newClassesid)
        Classes oldClasses = Classes.get(params.oldClassesid)
        
        def studentids = params.selectStudent.tokenize(',')
        log.info(studentids)
        studentids.each{
            Student s = Student.get(it.trim())
            s.classes = newClasses
            if (s.save(flush: true)) {
                return render(['success':true,'message' : '<font color="green"> 成功转移班级！</font>'] as JSON) 
            }
            else {
                return render(['success':false,'message' : '<font color="green"> 转移班级失败！</font>'] as JSON) 
            }
        }

//        def classesStudentList = Student.findAllByClasses(c)
//        log.info(classesStudentList.size())
//        // todo 此处的classes应该是某一个学校的，这里在没有Session的状况下就用全部
//        [classesStudentList:classesStudentList,classesInstance:c]        
    }
}
